package sybel.pan;

import javax.swing.JPanel;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import sybel.DOMPanelInterface;


public class Alternative extends javax.swing.JPanel implements DOMPanelInterface {

    /**
     * Il costruttore riceve un nodo "alternative" in input
     * @param node
     */
    public Alternative(Node node) {
        ParentFlow = node.getParentNode();
        alternative = (Element)node;
        initComponents();
        // se il nodo è già stato riempito viene chiamato il metodo che inizializza i campi dei pannelli
        if(alternative.getFirstChild() != null)
            initPanel();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextFlowName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextStepName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextID = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jCBAlternative = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("*Flow Name:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("StepName:");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("StepID:");

        jCBAlternative.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one...", "Condition", "Interaction" }));
        jCBAlternative.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBAlternativeItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 43, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Description:");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);
        jTextArea1.setLineWrap(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jTextFlowName, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextStepName, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextID, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCBAlternative, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 686, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 789, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFlowName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextStepName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCBAlternative, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(127, Short.MAX_VALUE))
        );

        jPanel1Layout.setHonorsVisibility(false);
        jPanel1.setVisible(false);
    }// </editor-fold>//GEN-END:initComponents

    private void jCBAlternativeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBAlternativeItemStateChanged
        AlternativeSelector();
    }//GEN-LAST:event_jCBAlternativeItemStateChanged

    public void initPanel(){
        setFlowName(alternative.getAttribute("flow"));
        setStepName(alternative.getAttribute("stepName"));
        setStepID(alternative.getAttribute("stepId"));
        if(alternative.getElementsByTagName("description").item(0) != null)
            setDescription(alternative.getElementsByTagName("description").item(0).getTextContent());

        NodeList nl = alternative.getElementsByTagName("condition");
        if(nl.item(0) != null){
            Condition condition = new Condition(nl.item(0)); // passo al costruttore il nodo condition
            setAlternativePanel(condition);
            jCBAlternative.setSelectedIndex(1);
        }

        nl = alternative.getElementsByTagName("interaction");
        if(nl.item(0) != null){
            Interaction interaction = new Interaction(nl.item(0));// passo al costruttore il nodo interaction
            setAlternativePanel(interaction);
            jCBAlternative.setSelectedIndex(2);
        }


    }

    private void AlternativeSelector(){
        switch(jCBAlternative.getSelectedIndex()){

            // Condition
            case 1:
                Condition condition = new Condition(alternative);
                setAlternativePanel(condition);
                break;

            // Interaction
            case 2:
                Interaction interaction = new Interaction(ParentFlow);
                setAlternativePanel(interaction);
                break;

            default:
                jPanel1.removeAll();
                jPanel1.repaint();
        }
    }


    private void setAlternativePanel(JPanel entity){

        jPanel1.removeAll();
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                );

        // rendo visibile il nuovo pannello
        jPanel1.repaint();
        jPanel1.setVisible(true);
    }

    public String getFlowName(){
        return jTextFlowName.getText();
    }

    public String getStepName(){
        return jTextStepName.getText();
    }

    public String getStepID(){
        return jTextID.getText();
    }

    public String getDescription(){
        return jTextArea1.getText();
    }

    public String getSelectedAlternative(){
        int temp =  jCBAlternative.getSelectedIndex();
        switch(temp){
            case 1:
                return "condition";
            case 2:
                return "interaction";

            default:
                return null;
        }
    }

    public void setFlowName(String s){
        jTextFlowName.setText(s);
    }

    public void setStepName(String s){
        jTextStepName.setText(s);
    }

    public void setStepID(String s){
        jTextID.setText(s);
    }

    public void setDescription(String s){
        jTextArea1.setText(s);
    }

    /**
     * Metodo che crea e restituisce il nodo "alternative" riempito con ciò che contiene il pannello
     * @return il nodo "alternative"
     */
    public Node saveData(){
        Document doc = ParentFlow.getOwnerDocument();
        alternative = doc.createElement("alternative");
        if(!getFlowName().isEmpty())
            alternative.setAttribute("flow",getFlowName());
        if(!getStepName().isEmpty())
            alternative.setAttribute("stepName",getStepName());
        if(!getStepID().isEmpty())
            alternative.setAttribute("stepId",getStepID());

        if(!getDescription().isEmpty()){
            Element Description = doc.createElement("description");
            Description.setTextContent(getDescription());
            alternative.appendChild(Description);
        }


        int temp = jCBAlternative.getSelectedIndex();
        switch(temp){
            case 1:
                Condition cond = (Condition)jPanel1.getComponents()[0];
                alternative.appendChild(cond.saveData());
                break;
            case 2:
                Interaction inter = (Interaction)jPanel1.getComponents()[0];
                alternative.appendChild(inter.saveData());
                break;
            default:
                break;
        }

        return (Node)alternative;
    }

    public Node AppendSavedData(){
        Node temp = alternative;
        Node newnode = saveData();
        ParentFlow.replaceChild(newnode, temp);
        return ParentFlow;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jCBAlternative;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextFlowName;
    private javax.swing.JTextField jTextID;
    private javax.swing.JTextField jTextStepName;
    // End of variables declaration//GEN-END:variables
    private Node ParentFlow;
    private Element alternative;

}
